(function(angular) {
	"use strict";
	angular.module("fs.waf").factory("settlementPaymentService", settlementPaymentService);
	settlementPaymentService.$inject = [ "$q", "ajaxServiceFactory", "settlementPaymentFactory", "addressInfoService", "settlePaySwiftService" ];
	function settlementPaymentService($q, ajaxServiceFactory, settlementPaymentFactory, addressInfoService, settlePaySwiftService) {
		function settlementPaymentOptions() {
			var ajaxService = ajaxServiceFactory.buildAjaxService({
				settlementPayment : {
					url : "do/cls/dataList/settlementPayment",
					method : "get"
				}
			}), defer = $q.defer(), map;
			ajaxService.settlementPayment().then(function(response) {
				map = response ? response.data : {};
				addressInfoService.initData().then(function(response) {
					map.countryOptionAll = response.countryOptionAll;
					map.stateOptionAll = response.stateOptionAll;
					map.cityOptionAll = response.cityOptionAll;
					map.stateList = response.stateList;
					map.cityList = response.cityList;
					// map.countryOptions = response.countryOptions;
					defer.resolve(map);
				});
			});
			return defer.promise;
		}

		function initData(settlement, layoutConfig) {
			var payPartyConfig = [];
			if (settlement.payDetails && settlement.payDetails.length > 0) {
				settlement.notExistPayment = false;
			} else {
				settlement.notExistPayment = true;
			}
			settlement.payDetailCache = settlement.payDetailCache || [];
			if (!settlement.notExistPayment) {
				angular.forEach(settlement.payDetails, function(payDetail, index, array) {
					settlement.payDetailCache["pd" + payDetail.payItemSeq] = payDetail;
					payDetail.paycodeName = payDetail.paycode ? payDetail.paycode.substr(7) : "";
					payPartyConfig["pd" + payDetail.payItemSeq] = settlementPaymentFactory.getFunction(payDetail.payCodeObj, "initPayment")(settlement, layoutConfig, payDetail);
				});
			}
			return payPartyConfig;
		}

		function getPayDetailDynaHeadConfig(payDetail, payDetailInfoConfig) {
			var dynaConfig = angular.copy(payDetailInfoConfig);
			if (payDetail.crDrFlag == "C") {
				dynaConfig.gridConfigs.payCcy.label = "label.settlement.payment.payccy";
				dynaConfig.gridConfigs.payAmt.label = "label.settlement.payment.payamount";
			} else {
				dynaConfig.gridConfigs.payCcy.label = "label.settlement.payment.receiveccy";
				dynaConfig.gridConfigs.payAmt.label = "label.settlement.payment.receiveamount";
			}
			return dynaConfig;
		}

		function getPayDetailsConfig(settlement, layoutConfig) {
			var result = {
				payDetailDynaHeadConfig : [],
				mailConfig : [],
				emailConfig : [],
				faxConfig : []
			};
			if (settlement && settlement.payDetails) {
				angular.forEach(settlement.payDetails, function(payDetail) {
					settlementPaymentFactory.getFunction(payDetail.payCodeObj, "getPayInfoConfig")(payDetail, result, layoutConfig);
					result.payDetailDynaHeadConfig["pd" + payDetail.payItemSeq] = getPayDetailDynaHeadConfig(payDetail, layoutConfig.formConfigs.payDetailInfo);
					result.mailConfig["pd" + payDetail.payItemSeq] = angular.copy(layoutConfig.formConfigs.mail);
					result.emailConfig["pd" + payDetail.payItemSeq] = angular.copy(layoutConfig.formConfigs.email);
					result.faxConfig["pd" + payDetail.payItemSeq] = angular.copy(layoutConfig.formConfigs.fax);
				});
			}
			return result;
		}

		function checkpayParties(settlement) {
			angular.forEach(settlement.payDetails, function(payDetail, index, array) {
				settlementPaymentFactory.getFunction(payDetail.payCodeObj, "checkpayParties")(payDetail);
			});
		}
		

		return {
			initData : initData,
			settlementPaymentOptions : settlementPaymentOptions,
			getPayDetailsConfig : getPayDetailsConfig,
			checkpayParties : checkpayParties,
			swiftTypeCallBack : settlePaySwiftService.swiftTypeCallBack,
			partyCountryChanged : settlePaySwiftService.partyCountryChanged,
			partyStateChanged : settlePaySwiftService.partyStateChanged,
			getPayPartyConfigs : settlePaySwiftService.getPayPartyConfigs,
			initPayPartyTabs : settlePaySwiftService.initPayPartyTabs
		};
	}
})(angular);
